package com.dxd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dxd.entity.PageEntity;
import com.dxd.entity.UserInfo;

public class UserInfoDao extends PublicDao<UserInfo> {

	@Override
	protected UserInfo result(ResultSet rs) throws SQLException {
		UserInfo userInfo = new UserInfo();
		while (rs.next()) {
			userInfo.setId(rs.getInt("id"));
			userInfo.setUserName(rs.getString("user_name"));
			userInfo.setPassWord(rs.getString("pass_word"));
			userInfo.setName(rs.getString("name"));
			userInfo.setSex(rs.getString("sex"));
			userInfo.setHeadImg(rs.getString("head_img"));
			userInfo.setPhone(rs.getString("phone"));
			userInfo.setAddress(rs.getString("address"));
			userInfo.setCreateTime(rs.getString("create_time"));
			userInfo.setIsAdmin(rs.getInt("is_admin"));
			userInfo.setState(rs.getInt("state"));
			userInfo.setProvince(rs.getString("province"));
			userInfo.setCity(rs.getString("city"));
			userInfo.setArea(rs.getString("area"));
			userInfo.setAge(rs.getInt("age"));
		}
		return userInfo;
	}

	@Override
	protected List<UserInfo> results(ResultSet rs) throws SQLException {
		List<UserInfo> list = new ArrayList<UserInfo>();
		while (rs.next()) {
			UserInfo userInfo = new UserInfo();
			userInfo.setId(rs.getInt("id"));
			userInfo.setUserName(rs.getString("user_name"));
			userInfo.setPassWord(rs.getString("pass_word"));
			userInfo.setName(rs.getString("name"));
			userInfo.setSex(rs.getString("sex"));
			userInfo.setHeadImg(rs.getString("head_img"));
			userInfo.setPhone(rs.getString("phone"));
			userInfo.setAddress(rs.getString("address"));
			userInfo.setCreateTime(rs.getString("create_time"));
			userInfo.setIsAdmin(rs.getInt("is_admin"));
			userInfo.setState(rs.getInt("state"));
			userInfo.setProvince(rs.getString("province"));
			userInfo.setCity(rs.getString("city"));
			userInfo.setArea(rs.getString("area"));
			userInfo.setAge(rs.getInt("age"));
			list.add(userInfo);
			userInfo = null;
		}
		return list;
	}

	/**
	 * 登录验证
	 * 
	 * @param userName
	 * @param passWord
	 * @return
	 */
	public UserInfo Login(String userName, String passWord, Integer isAdmin) {
		List<Object> objs = new ArrayList<Object>();
		objs.add(userName);
		objs.add(passWord);
		objs.add(isAdmin);
		String sql = "select * from user_info where user_name=? and pass_word=? and is_admin=?";
		return queryOne(sql, objs);
	}

	/**
	 * 根据登录名查询用户
	 * 
	 * @param userName
	 * @return
	 */
	public UserInfo queryByUserName(String userName) {
		List<Object> objs = new ArrayList<Object>();
		objs.add(userName);
		String sql = "select * from user_info where user_name=?";
		return queryOne(sql, objs);
	}

	/**
	 * 删除用户
	 * 
	 * @param ids
	 * @return
	 */
	public int deleteByUserIds(String ids) {
		StringBuffer sql = new StringBuffer("DELETE FROM user_info WHERE id in(");
		sql.append(ids);
		sql.append(")");
		return AddOrRemoveOrUpdate(sql.toString(), null);
	}

	/**
	 * 添加用户
	 * 
	 * @param userInfo
	 * @return
	 */
	public int addUser(UserInfo userInfo) {
		String sql = "INSERT INTO user_info(user_name,pass_word,name,sex,head_img,phone,province,city,area,address,is_admin,state,age) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> objs = new ArrayList<Object>();
		objs.add(userInfo.getUserName());
		objs.add(userInfo.getPassWord());
		objs.add(userInfo.getName());
		objs.add(userInfo.getSex());
		objs.add(userInfo.getHeadImg());
		objs.add(userInfo.getPhone());
		objs.add(userInfo.getProvince());
		objs.add(userInfo.getCity());
		objs.add(userInfo.getArea());
		objs.add(userInfo.getAddress());
		objs.add(userInfo.getIsAdmin());
		objs.add(userInfo.getState());
		objs.add(userInfo.getAge());
		return AddOrRemoveOrUpdate(sql, objs);
	}

	/**
	 * 修改用户
	 * 
	 * @param userInfo
	 * @return
	 */
	public int updateUser(UserInfo userInfo) {
		String sql = "UPDATE user_info SET user_name=?,pass_word=?,name=?,sex=?,head_img=?,phone=?,province=?,city=?,area=?,address=?,is_admin=?,state=?,age=? WHERE id=?";
		List<Object> objs = new ArrayList<Object>();
		objs.add(userInfo.getUserName());
		objs.add(userInfo.getPassWord());
		objs.add(userInfo.getName());
		objs.add(userInfo.getSex());
		objs.add(userInfo.getHeadImg());
		objs.add(userInfo.getPhone());
		objs.add(userInfo.getProvince());
		objs.add(userInfo.getCity());
		objs.add(userInfo.getArea());
		objs.add(userInfo.getAddress());
		objs.add(userInfo.getIsAdmin());
		objs.add(userInfo.getState());
		objs.add(userInfo.getAge());
		objs.add(userInfo.getId());
		return AddOrRemoveOrUpdate(sql, objs);
	}

	/**
	 * 查询单个用户
	 * 
	 * @param id
	 * @return
	 */
	public UserInfo findById(Integer id) {
		List<Object> objs = new ArrayList<Object>();
		objs.add(id);
		String sql = "select * from user_info where id=?";
		return queryOne(sql, objs);
	}

	/**
	 * 查询用户列表
	 * 
	 * @param page
	 * @param size
	 * @param userName
	 * @param name
	 * @return
	 */
	public PageEntity<UserInfo> queryList(int page, int size, String userName, String name) {
		PageEntity<UserInfo> pageEntity = new PageEntity<UserInfo>();
		StringBuffer sql = new StringBuffer("SELECT * FROM user_info where 1=1 ");
		StringBuffer countsql = new StringBuffer("SELECT COUNT(1) as count FROM user_info where 1=1 ");
		if (userName != null && !"".equals(userName)) {
			sql.append(" and user_name like'%");
			sql.append(userName);
			sql.append("%'");
			countsql.append(" and user_name like'%");
			countsql.append(userName);
			countsql.append("%'");

		}
		if (name != null && !"".equals(name)) {
			sql.append(" and name like'%");
			sql.append(name);
			sql.append("%'");
			countsql.append(" and name like'%");
			countsql.append(name);
			countsql.append("%'");
		}
		int count = queryCount(countsql.toString(), null);
		sql.append(" LIMIT ");
		sql.append((page - 1) * size);
		sql.append(",");
		sql.append(size);
		List<UserInfo> list = queryList(sql.toString(), null);
		pageEntity.setPage(page);
		pageEntity.setSize(size);
		pageEntity.setList(list);
		pageEntity.setTotalCount(count);
		return pageEntity;
	}

}
